package com.hxh.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.hxh.entity.Project;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.TemplateFieldConfig;
import com.hxh.entity.input.TemplateFieldConfigInput;
import com.hxh.entity.output.ApiResult;
import com.hxh.service.ProjectService;
import com.hxh.service.TemplateConfigService;
import com.hxh.service.TemplateFieldConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/templateFieldConfig")
@Slf4j
public class TemplateFieldConfigController {
    @Autowired TemplateFieldConfigService templateFieldConfigService;
    @Autowired
    ProjectService projectService;
    @Autowired
    TemplateConfigService templateConfigService;

    @PostMapping(value = "getByProjectId")
    public ApiResult getByProjectId(@RequestParam Integer projectId) {
        Project project = projectService.selectById(projectId);
        if (project == null) {
            return ApiResult.fail("项目不存在！");
        }
        String template = project.getTemplate();
        TemplateConfig templateConfigByName = templateConfigService.getTemplateConfigByName(template);
        List<TemplateFieldConfig> list = templateFieldConfigService.getTemplateFieldConfigByTemplateId(templateConfigByName.getId());
        if (CollectionUtils.isEmpty(list)) {
            return ApiResult.fail("未查询到模板字段映射配置数据");
        }
        return ApiResult.ok("查询成功", list);
    }

    //获取模板可配置的字段映射类型管理分页
    @RequestMapping(value = "/listPage", produces = "application/json;charset=utf-8")
    public ApiResult listPage(TemplateFieldConfigInput pojo) {
        Page<TemplateFieldConfig> page = new Page<>(pojo.getCurrent(), pojo.getPageSize());
        templateFieldConfigService.listPage(page, pojo);
        return ApiResult.ok("获取模板可配置的字段映射类型管理分页成功", page);
    }

    //新增模板可配置的字段映射类型管理
    @PostMapping(value = "add")
    public ApiResult add(TemplateFieldConfigInput templateFieldConfig) {

        boolean flag = templateFieldConfigService.insert(templateFieldConfig);
        if (flag) {
            return ApiResult.ok("保存成功");
        }
        return ApiResult.fail("保存失败");
    }

    //修改模板可配置的字段映射类型管理
    @PostMapping(value = "update")
    public ApiResult update(TemplateFieldConfigInput templateFieldConfig) {
        boolean flag = templateFieldConfigService.update(templateFieldConfig);
        if (flag) {
            return ApiResult.ok("更新成功");
        } else {
            return ApiResult.fail("更新失败");
        }
    }

    //删除模板可配置的字段映射类型管理
    @PostMapping(value = "delete")
    public ApiResult deleteById(@RequestParam Integer id ) {
        boolean flag = templateFieldConfigService.deleteById(id );
        if (flag) {
            return ApiResult.ok("删除成功");
        } else {
            return ApiResult.fail("删除失败");
        }
    }

}
